//
//  VisitorLoginView.swift
//  weibo02
//
//  Created by 刘涛 on 15/6/23.
//  Copyright © 2015年 tao6. All rights reserved.
//

import UIKit
// 协议
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    func VisitorRegisterButtonClick()
    func VisitorLoginButtonClick()
}

class VisitorLoginView: UIView {

    
    weak var delegate: VisitorLoginViewDelegate?
    
    
    
    
    func setupViewInfo(iconName: String, message: String, isHome: Bool) {
        messageLabel.text = message
        
        if isHome {
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        }else {
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)
            
            // 将视图层次结构置后
            sendSubviewToBack(maskIconView)
        }
        
    }
    
    // 首页动画
    func startAnmiation() {
        let anim = CABasicAnimation(keyPath: "transform,rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 90
        anim.toValue = 2 * M_PI
        
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    // 用户注册
    func register() {
//        print("注册")
        delegate?.VisitorRegisterButtonClick()
    }
    
    func login() {
//        print("登录")
        delegate?.VisitorLoginButtonClick()
    }
    
    
    // 默认构造方法，创建控件，并且指定布局
    // 用xib & sb 不会掉用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    
    // 解档，用xib & sb开发会调用
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 创建控件
    private func setupUI() {
        
        // 1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.自动布局
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        // views 设置vfl 中的控件名称和实际控件对应关系
        // width
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": messageLabel]))
        //height
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": messageLabel]))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": registerButton]))
        //height
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": registerButton]))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": loginButton]))
        //height
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": loginButton]))
        
        // 遮罩视图
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        
        // 水平方向
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": maskIconView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]-160-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView": maskIconView]))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
        
//       addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50))
//        

    }
    
    
    // MARK: - 懒加载控件
    
    lazy var iconView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")

        return UIImageView(image: image)
    }()
    
    // 遮罩图片
    lazy var maskIconView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
        }()
    
    
    lazy var homeIconView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_house")
        
        return UIImageView(image: image)
    }()

    // 文字标签
    lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        label.preferredMaxLayoutWidth = 224
        label.numberOfLines = 2
        label.sizeToFit()
        return label
    }()

    // 注册按钮
    lazy var registerButton: UIButton = {
        
        let btn = UIButton()
        
        btn.bounds = CGRectMake(0, 0, 100, 40)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    // 登录按钮
    lazy var loginButton: UIButton = {

        let btn = UIButton()
        
        btn.bounds = CGRectMake(0, 0, 100, 40)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn

        
    }()
    
    
    
    

    

}
